<?php

/**
 * Dos form.
 *
 * @package    Amman to London
 * @subpackage form
 * @author     Adel Abu-Saif <adeljas@msn.com>
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class DosForm extends BaseDosForm {

    public function configure() {
//      $from_admin = sfContext::getInstance()->getConfiguration()->getApplication() == 'admin';
//      
//      if($from_admin){
        unset($this['times_rated']);
        unset($this['created_at']);
        unset($this['updated_at']);
        unset($this['preview_zip']);
        $this->widgetSchema['rating'] = new asWidgetReadOnly();
        $this->widgetSchema['cover_image'] = new sfWidgetFormInputFile();
        $this->validatorSchema['cover_image'] = new sfValidatorFile(
                        array(
                            'path' => sfConfig::get('sf_upload_dir') . '/doseyeh/'
                            , 'required' => $this->getObject()->isNew()
                        )
        );
        $this->widgetSchema['file'] = new sfWidgetFormInputFile();
        $this->validatorSchema['file'] = new sfValidatorFile(
                        array(
                            'path' => sfConfig::get('sf_upload_dir') . '/doseyeh/file/'
                            , 'required' =>false
                        )
        );
        
        $this->widgetSchema['zip'] = new sfWidgetFormInputFile();
        $this->validatorSchema['zip'] = new sfValidatorFile(
                        array(
                            'path' => sfConfig::get('sf_upload_dir') . '/doseyeh/zip/'
                            , 'required' => $this->getObject()->isNew()
                        )
        );
//        $this->widgetSchema['preview_zip'] = new sfWidgetFormInputFile();
//        $this->validatorSchema['preview_zip'] = new sfValidatorFile(
//                        array(
//                            'path' => sfConfig::get('sf_upload_dir') . '/doseyeh/preview_zip/'
//                            , 'required' => false
//                        )
//        );
        $this->validatorSchema['name_en']->setOption('required', true);
        $this->validatorSchema['name_ar']->setOption('required', true);
        $this->widgetSchema->setLabels(array(
            'name_en' => 'Doseyeh Name (English)'
            , 'name_ar' => 'Doseyeh Name (Arabic)'
        ));
//      }       
//      

        
//        $this->widgetSchema['name_en']=new sfWidgetFormInputText();
//        $this->widgetSchema['name_ar']=new sfWidgetFormInputText();
//      
//        unset($this['name_en']);
//        unset($this['name_ar']);
    }

    public function save($con = null) {
        ini_set('max_execution_time', 0);
        $user = $this->getUser();
        $cfi = $user->getFromFilters('course_faculty_id', 'dos');
        if ($cfi)
            $this->object->set('course_faculty_id', $cfi);

        $obj = parent::save($con);
        $zip = $obj->get('zip');
        // $preview_zip = $obj->get('preview_zip');
        $zfname = explode('.', $obj->get('zip'));
        $zfname = $zfname [0];

        //  $pzfname = explode('.', $obj->get('preview_zip'));
        //  $pzfname = $pzfname [0];

        $zip_path = sfConfig::get('sf_upload_dir') . '/doseyeh/zip/' . $zip;
        // $preview_zip_path = sfConfig::get('sf_upload_dir') . '/doseyeh/preview_zip/' . $preview_zip;
        if ($zip != '')
            $this->unzip($zip_path, sfConfig::get('sf_upload_dir') . '/doseyeh/zip/' . $zfname . '/');
//        if ($preview_zip != '')
//            $this->unzip($preview_zip_path, sfConfig::get('sf_upload_dir') . '/doseyeh/preview_zip/' . $pzfname . '/');
        return $obj;
    }

    function unzip($input_file_path, $extract_path) {
        $zip = new ZipArchive;
        $res = $zip->open($input_file_path);
        if ($res === TRUE) {
            $zip->extractTo($extract_path);
            $zip->close();
        }
    }

}
